# -*- coding: UTF-8 -*-
"""
@Author  ：秋枫zsp
@Email   ：370172879@qq.com
@Date    ：2024/11/15 10:38 
@Desc    ：
"""
from multiprocessing.dummy import Pool

from core.http_client import HttpClient
from core.db_utils import MysqlManager

http_client = HttpClient()
db_manager = MysqlManager()


def get_detail(exhibitor):
    _id = exhibitor.get('_id')
    url = f'https://personal-account.expovr.ru/api/exhibitors/' + _id
    response = http_client.get(url)
    datas = response.json()

    city = datas.get('exhibitor', {}).get('contacts', {}).get('city', {}).get('en')
    country = datas.get('exhibitor', {}).get('contacts', {}).get('country', {})
    phone = datas.get('exhibitor', {}).get('contacts', {}).get('phone', {})
    email = datas.get('exhibitor', {}).get('contacts', {}).get('email', {})
    www = datas.get('exhibitor', {}).get('contacts', {}).get('www', {})
    stand = datas.get('exhibitor', {}).get('stand', {})
    activities = datas.get('exhibitor', {}).get('activities', [])
    categories = datas.get('exhibitor', {}).get('categories', [])
    act = ''
    for activity in activities:
        name = activity.get('name', {}).get('en')
        # act_descr = activity.get('descr', {}).get('en')
        # speaker = activity.get('speaker', {}).get('en')
        date = activity.get('date', {})
        _act = f"date:{date} name:{name}"
        act = act + _act + '\n'
    category = ''
    for _ in categories:
        name = _.get('name', {}).get('en')
        category = category + name + '\n'
    brief = datas.get('exhibitor', {}).get('brief', {}).get('en')
    about = datas.get('exhibitor', {}).get('about', {}).get('en')
    descr = datas.get('exhibitor', {}).get('descr', {}).get('en')
    brand = datas.get('exhibitor', {}).get('brand', {})
    print(city, country, phone, email, www, stand, act, category, brief, about, descr, brand)
    sql = ('insert into pernal_account '
           '(id,city, country, phone, email, www, stand, act, category, brief, about, descr, brand) '
           'values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)')
    db_manager.execute_sql(sql,
                           (_id, city, country, phone, email, www, stand, act, category, brief, about, descr, brand)
                           )


def get_list():
    for i in range(1, 118):
        print(f'正在爬取页数：{i}')
        url = f"https://personal-account.expovr.ru/api/exhibitors/search?find=&skip={(i - 1) * 12}&limit=12&event=661cdcb8a32e1aa48ed012aa&dates=&categories=&countries=&lang=en&where=index"
        response = http_client.get(url)
        datas = response.json()
        exhibitors = datas.get('exhibitors')
        # for exhibitor in exhibitors:
        #     _id = exhibitor.get('_id')
        #     get_detail(_id)
        pool = Pool(12)
        pool.map(get_detail, exhibitors)
        pool.close()
        pool.join()
